﻿@page "/SensorCard/{id:int}"


@using System.Collections.Concurrent
@using TrackSystem.Entities.Dtos.Sensor
@using TrackSystem.Entities.Extensions
@using TrackSystem.UI.Server.HttpRepository.Sensor
@using TrackSystem.UI.Server.Units

<h1>传感器列表</h1>

<Divider class="mt-3 mb-3" />

<div class="row g-3">

    @* 已有传感器显示 *@
    @foreach (var sensor in sensors ?? Enumerable.Empty<SensorWithChannelDto>())
    {
        <div class="col-12 col-sm-6 col-lg-4 col-xl-2">
            <Card Color="Color.Info" IsCenter="true">
                <HeaderTemplate>
                    传感器名称：@sensor.Name
                </HeaderTemplate>

                <BodyTemplate>
                    @if (changedStatus.TryGetValue(sensor.Id, out var isChanged) && isChanged)
                    {
                        <SensorForUpdate Sensor="sensor" />
                    }
                    else
                    {
                        <SensorForView sensor="sensor" />
                    }
                </BodyTemplate>

                <FooterTemplate>
                     @if (changedStatus.TryGetValue(sensor.Id, out var isChanged) && isChanged)
                     {
                        <Button class="btn btn-primary" OnClick="@(() => OnChanged(sensor.Id))">
                            <i class="fa-solid fa-check"></i>
                        </Button>
                        <Button class="btn btn-primary" OnClick="@(() => OnChangedCancel(sensor.Id))">
                            <i class="fa-solid fa-xmark"></i>
                        </Button>
                    }
                    else
                    {
                        <Button class="btn btn-primary" OnClick="@(() => OnStartChange(sensor.Id))">
                            <i class="bi bi-gear"></i>
                        </Button>
                    }
                    @if (Id == 0)
                    {
                        <PopConfirmButton Placement="Placement.Top"
                                                  Color="Color.Danger"
                                         ConfirmIcon="fa-solid fa-triangle-exclamation text-danger"
                                         ConfirmButtonColor="Color.Danger"
                                         Text=""
                                         Content="确定删除数据吗？"
                                         OnConfirm="@(() => OnDeleted(sensor.Id))">
                        <i class="fa-solid fa-delete-left"></i>
                        </PopConfirmButton>
                    }
                    else
                    {
                        <Button class="btn btn-danger" OnClick="@(() => OnComBack())">
                            <i class="bi bi-arrow-90deg-up"></i>
                        </Button>
                    }
                </FooterTemplate>
            </Card>
        </div>
    }

    @* 新增传感器 *@
    @if (Id == 0)
    {
        <div class="col-12 col-sm-6 col-lg-4 col-xl-2">
            <Card Color="Color.Primary" IsCenter="true">
                <HeaderTemplate>
                    新增传感器
                </HeaderTemplate>

                <BodyTemplate>
                    @if (IsInsertMode && sensorForInsert != null)
                    {
                    <SensorForUpdate Sensor="sensorForInsert" />
                    }
                    else
                    {
                    <img src="images/add.jpg" width="100" height="100" onclick="@OnStartInsert"/>
                    }
                </BodyTemplate>
                <FooterTemplate>
                    @if (IsInsertMode && sensorForInsert != null)
                    {
                    <Button class="btn btn-primary" OnClick="@OnFinishInsert">
                            <i class="fa-solid fa-check"></i>
                        </Button>
                        <Button class="btn btn-secondary" OnClick="@(() => IsInsertMode = false)">
                            <i class="fa-solid fa-xmark"></i>
                        </Button>
                    }
                </FooterTemplate>
            </Card>
        </div>
    }

</div>

<Divider class="mt-3 mb-3" />

@* 分页组件 *@
<Pagination PageCount="@sensors.Count()" OnPageLinkClick="@OnPageClick" Alignment="Alignment.Center" class="mt-3" />
